package pit.com.ordermobilesystem.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.util.Utils;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import at.technikum.mti.fancycoverflow.FancyCoverFlow;
import at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter;

public class ImageFancyCoverFlowAdapter extends FancyCoverFlowAdapter{

	JSONArray images;	
	DisplayImageOptions options;	
	public ImageLoader imageLoader = ImageLoader.getInstance();	
	int width=300;
	int height=300;
    public ImageFancyCoverFlowAdapter(Context mContext,JSONArray images,int width,int height) {
		super();
		this.images = images;
		this.width=width;
		this.height=height;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_no_image)
		.showImageForEmptyUri(R.drawable.ic_no_image)
		.showImageOnFail(R.drawable.ic_no_image)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();			
	}

	@Override
    public int getCount() {
        return images.length();
    }

    @Override
    public String getItem(int i) {
        try {
			return images.getString(i);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return "";
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
        ImageView imageView = null;

        if (reuseableView != null) {
            imageView = (ImageView) reuseableView;
        } else {
            imageView = new ImageView(viewGroup.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setLayoutParams(new FancyCoverFlow.LayoutParams(width, height));

        }

        try {
			imageLoader.displayImage(Utils.getPathImage(images.getString(i)), imageView, options,null);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return imageView;
    }

}
